package com.acme.bankapp.service;

import java.io.IOException;

import com.acme.bankapp.dal.BankDataService;
import com.acme.bankapp.domain.account.CheckingAccount;
import com.acme.bankapp.domain.account.SavingAccount;
import com.acme.bankapp.domain.bank.Bank;
import com.acme.bankapp.domain.client.Client;
import com.acme.bankapp.domain.client.Gender;
import com.acme.bankapp.server.BankServer;
import com.martiansoftware.jsap.FlaggedOption;
import com.martiansoftware.jsap.JSAP;
import com.martiansoftware.jsap.JSAPResult;
import com.martiansoftware.jsap.Switch;

public class BankApplication {

	public static void main(String[] args) throws Exception {
		JSAP jsap = new JSAP();

		jsap.registerParameter(new FlaggedOption("feed_file")
				.setShortFlag(JSAP.NO_SHORTFLAG)
				.setLongFlag("loadfeed")
				.setStringParser(JSAP.STRING_PARSER)
				.setHelp("Path to a feed file (*.feed)."));

		jsap.registerParameter(new FlaggedOption("ser_file")
				.setShortFlag(JSAP.NO_SHORTFLAG)
				.setLongFlag("loadbank")
				.setStringParser(JSAP.STRING_PARSER)
				.setHelp("Path to a bank data file (*.ser)."));

		jsap.registerParameter(new Switch("server")
				.setShortFlag(JSAP.NO_SHORTFLAG)
				.setLongFlag("server")
				.setHelp("Start server."));
		
		jsap.registerParameter(new Switch("sampledata")
				.setShortFlag(JSAP.NO_SHORTFLAG)
				.setLongFlag("sampledata")
				.setHelp("Load sample data."));
		
		JSAPResult config = jsap.parse(args);

		if ( !config.success() ) {
			printUsage(jsap);
			return;
		}
		
		run(config);
	}
	
	private static void run(JSAPResult config) throws Exception {
		Bank bank;
		if (config.getString("ser_file") != null) {
			bank = BankDataService.readBankFromFile(config.getString("ser_file"));
		} else {
			bank = new Bank();
		}
		
		if (config.getString("feed_file") != null) {
			BankDataService.loadFeedFromFile(bank, config.getString("feed_file"));
		}
		
		if (config.getBoolean("sampledata")) {
			loadSampleData(bank);
		}
		
		if (config.getBoolean("server")) {
			BankService.enableEmailNotifications(bank);
			startServer(bank);
		}
		
	}

	private static void loadSampleData(Bank bank) {
		Client client;
		
		try {
			client = new Client("Todd", Gender.MALE);
			client.addAccount(new SavingAccount(100));
			client.addAccount(new CheckingAccount(-20, 50));
			BankService.addClient(bank, client);
			
			client = new Client("Lovett", Gender.FEMALE);
			client.addAccount(new SavingAccount(200));
			BankService.addClient(bank, client);
			
			client = new Client("Turpin", Gender.MALE);
			client.addAccount(new CheckingAccount(-30, 75));
			BankService.addClient(bank, client);
		} catch (Exception ignore) {}
	}

	private static void startServer(Bank bank) throws IOException {
		(new BankServer(bank)).serve();
	}

	private static void printUsage(JSAP jsap) {
		System.out.printf("Usage: java %s%n", BankApplication.class.getName());
		System.out.printf("        %s%n", jsap.getUsage());
		System.out.println();
		System.out.println(jsap.getHelp());
	}
}